home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / IMAGEFILE.PY < prev    next >
Encoding:
Python Source  |  2000-11-21  |  6.7 KB  |  169 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. """Image object that is stored in a file"""
  65.  
  66. __version__='$Revision: 1.9.36.2 $'[11:-2]
  67.  
  68. from OFS.content_types import guess_content_type
  69. from Globals import package_home
  70. from Common import rfc1123_date
  71. from string import rfind, split
  72. from DateTime import DateTime
  73. from time import time
  74. from os import stat
  75. import Acquisition
  76. import string
  77.  
  78.  
  79. class ImageFile(Acquisition.Explicit):
  80.     """Image objects stored in external files."""
  81.  
  82.     def __init__(self,path,_prefix=None):
  83.         if _prefix is None: _prefix=SOFTWARE_HOME
  84.         elif type(_prefix) is not type(''):
  85.             _prefix=package_home(_prefix)
  86.         path='%s/%s' % (_prefix, path)
  87.         self.path=path
  88.  
  89.         file=open(path, 'rb')
  90.         data=file.read()
  91.         file.close()
  92.         content_type, enc=guess_content_type(path, data)
  93.         if content_type:
  94.             self.content_type=content_type
  95.         else:
  96.             self.content_type='image/%s' % path[rfind(path,'.')+1:]
  97.         self.__name__=path[rfind(path,'/')+1:]
  98.         self.lmt=float(stat(path)[8]) or time()
  99.         self.lmh=rfc1123_date(self.lmt)
  100.  
  101.  
  102.     def index_html(self, REQUEST, RESPONSE):
  103.         """Default document"""
  104.         # HTTP If-Modified-Since header handling. This is duplicated
  105.         # from OFS.Image.Image - it really should be consolidated
  106.         # somewhere...
  107.         header=REQUEST.get_header('If-Modified-Since', None)
  108.         if header is not None:
  109.             header=string.split(header, ';')[0]
  110.             # Some proxies seem to send invalid date strings for this
  111.             # header. If the date string is not valid, we ignore it
  112.             # rather than raise an error to be generally consistent
  113.             # with common servers such as Apache (which can usually
  114.             # understand the screwy date string as a lucky side effect
  115.             # of the way they parse it).
  116.             try:    mod_since=long(DateTime(header).timeTime())
  117.             except: mod_since=None
  118.             if mod_since is not None:
  119.                 if getattr(self, 'lmt', None):
  120.                     last_mod = long(self.lmt)
  121.                 else:
  122.                     last_mod = long(0)
  123.                 if last_mod > 0 and last_mod <= mod_since:
  124.                     RESPONSE.setStatus(304)
  125.                     return ''
  126.  
  127.         RESPONSE.setHeader('Content-Type', self.content_type)
  128.         RESPONSE.setHeader('Last-Modified', self.lmh)
  129.         f=open(self.path,'rb')
  130.         data=f.read()
  131.         f.close()
  132.         return data
  133.  
  134.     HEAD__roles__=None
  135.     def HEAD(self, REQUEST, RESPONSE):
  136.         """ """
  137.         RESPONSE.setHeader('Content-Type', self.content_type)
  138.         RESPONSE.setHeader('Last-Modified', self.lmh)
  139.         return ''
  140.  
  141.     def __len__(self):
  142.         # This is bogus and needed because of the way Python tests truth.
  143.         return 1 
  144.  
  145.     def __str__(self):
  146.         return '<IMG SRC="%s" ALT="%s">' % (self.__name__, self.title_or_id()) 
  147.  
  148.